home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / monax25 / average.c < prev    next >
Text File  |  1987-10-18  |  3KB  |  163 lines

  1. /* 
  2.  
  3.    average.c - Average records output by report.exe 
  4.    This module is part of average.exe
  5.                   
  6.    Language = Microsoft C version 4.0
  7.  
  8.    This source is distributed freely and may be copied and
  9.    redistributed with the following provisos:
  10.    
  11.            You may not sell it, nor may you charge for making 
  12.            copies beyond the actual cost of mailing and media.
  13.                       
  14.    Written by Skip Hansen WB6YMH and Harold Price NK6K.
  15.  
  16.    Feedback is desired.
  17.  
  18.    RCP/M (213) 541-2503 300/1200/2400 baud
  19.    or via packet WB6YMH @ WB6YMH-2 or 
  20.          NK6K @ NK6K
  21.  
  22.    Modification history:
  23.  
  24.     8/10/87         NK6K: Initial release.    
  25.     ver 1.0         
  26.  
  27.    10/18/87     NK6K: First general release.
  28.    ver 1.1
  29.  
  30.     Notes:
  31.  
  32.    This program reads stdin, averages the values in the records,
  33.    and writes the output to stdout.
  34.  
  35.    All records are assumed to have the same number of fields that the
  36.    first record has, all entries are separated by commas.  Fields are
  37.    assumed to be type long.
  38.  
  39.    If the "t" option is present, the first field is assumed to be a
  40.    time stamp and is not passed through.  The n option gives the number of
  41.    record to average, e.g. -n5 means average 5 records.
  42.  
  43.  
  44. */
  45. #include <ctype.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48.  
  49. char fbuf[1024];
  50. int time_opt=0;
  51. int nfield=0;
  52. int nrecord=0;
  53. int avg_record=0;
  54. long array[100];
  55.  
  56.  
  57. char * 
  58. next_field(s)
  59. char *s;
  60. {
  61.     while ((*s!='\0') && (*s != ',')) s++;
  62.     if (*s=='\0') return(NULL);
  63.     else return(s+1);
  64. }
  65.  
  66. dump_it()
  67. {
  68. int i,j;
  69. long tmpl;
  70.  
  71.     if (time_opt) {
  72.         printf("%lu,",array[0]);
  73.         i=1;
  74.         }
  75.     else i=0;
  76.  
  77.     for (j=i;j<nfield-1;j++) {
  78.         tmpl = (array[j]*10) / (nrecord);
  79.         if ((tmpl % 10)>=5) tmpl = (tmpl/10)+1;
  80.         else tmpl=tmpl/10;
  81.         printf("%lu,",tmpl);
  82.         array[j]=0;
  83.         }
  84.  
  85.     tmpl = (array[nfield-1]*10) / (nrecord);
  86.     if ((tmpl % 10)>=5) tmpl = (tmpl/10)+1;
  87.     else tmpl=tmpl/10;
  88.     printf("%lu\n",tmpl );
  89.     array[nfield-1]=0;
  90.     nrecord=0;
  91.  
  92.     }
  93.     
  94.     
  95.  
  96.  
  97.  
  98. main(argc,argv)
  99. int argc;
  100. char *argv[];
  101. {
  102.  
  103. int i;
  104. char *s;
  105.  
  106.     for(i=0;i<100;i++) array[i]==0;
  107.  
  108.     /* get options */
  109.  
  110.     if (argc<2) {
  111.         fprintf(stderr,"Usage: average -nnum [-t]\n");
  112.         fprintf(stderr,"       t = first field is timestamp\n"); 
  113.         fprintf(stderr,"       num specifies number of records to average\n");
  114.         exit(1);
  115.         }
  116.  
  117.     for (i=1;i<argc;i++) {
  118.         if (*argv[i]=='-') *argv[i]++;
  119.         if (tolower(*argv[i])=='t') time_opt=1;
  120.         else if (tolower(*argv[i])=='n') avg_record=atoi(argv[i]+1);
  121.         }
  122.     
  123.     if (avg_record==0) {
  124.         fprintf(stderr,"Must specify n option\n");
  125.         exit(1);
  126.         }
  127.  
  128.  
  129. /* read data till done */
  130.  
  131.     nrecord=0;    /* number of records in this iteration */
  132.  
  133.     while(1) { 
  134.  
  135.         /* process data */
  136.  
  137.         if (gets(fbuf)==NULL) {
  138.             if (nrecord>0) dump_it();
  139.             break;
  140.             }
  141.         nfield=0;
  142.  
  143.         s=fbuf;
  144.         if ( time_opt) {
  145.             if (nrecord==0) 
  146.                 array[0]=atol(s);
  147.             s=next_field(s);
  148.             nfield++;
  149.             }
  150.  
  151.         while (s!=NULL) {
  152.             array[nfield++]+=atol(s);
  153.             s=next_field(s);
  154.             }
  155.  
  156.         if (++nrecord==avg_record) dump_it();
  157.  
  158.         }
  159.     }
  160.  
  161.  
  162.  
  163.